package com.jd.model;

public class Url {

	private String file = "";
	private String path = "";
	private String host = "";
	private String query = "";
	private String protocol = "";
	private String url = "";

	public String getUrl() {
		return url;
	}

	public Url(String url) {
		try {
			if (url == null) {
				return;
			}
			this.url = url;
			int p = url.indexOf("://");
			// protocol
			if (p != -1) {
				protocol = url.substring(0, p);
			} else {
				protocol = "";
			}

			// host
			if (p != -1) {
				int f = url.indexOf("/", p + 3);
				String host2port = "";
				if (f != -1) {
					host2port = url.substring(p + 3, f);
				} else {
					host2port = url.substring(p, url.length());
				}

				int d = host2port.indexOf(":");
				if (d != -1) {
					host = host2port.substring(0, d);
				} else {
					host = host2port;
				}

				// file
				if (f != -1) {
					int r = url.indexOf("#");
					if (r != -1) {
						file = url.substring(f, r);
					} else {
						file = url.substring(f, url.length());
					}
				} else {
					file = "";
				}

				// path
				int q = file.indexOf('?');
				if (q != -1) {
					path = file.substring(0, q);
				} else {
					path = file;
				}

				// query
				if (q != -1) {
					query = file.substring(q + 1);
				} else {
					query = "";
				}
			}
		} catch (StringIndexOutOfBoundsException e) {
		}
	}

	public String getFile() {
		return file;
	}

	public String getPath() {
		return path;
	}

	public String getHost() {
		return host;
	}

	public String getQuery() {
		return query;
	}

	public String getProtocol() {
		return protocol;
	}

}
